package com.sunlight.boot;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.sunlight.boot.codegen.MyCodeGen;

import java.util.Scanner;

public class CodeGen {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        MyCodeGen codeGen = new MyCodeGen();
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        //TODO 编辑数据源
        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        codeGen.setDataSource(dsc);

        //TODO 编辑包名，替换com.niceloo.boot
        codeGen.setPackageInfo(new PackageConfig().setParent("com.sunlight.boot")/*.setModuleName(scanner("模块名"))*/);

        codeGen.getGlobalConfig()
                .setOpen(false)
                //TODO 用于注解中的操作者@author
                .setAuthor("ChangYongjin")
                //TODO 设置项目的绝对路径
                .setOutputDir("E:\\workspace\\study-space\\mybaits-plus-mysql-demo\\src\\main\\java");

        codeGen.getStrategy()
//                .setSuperControllerClass(BaseController.class) // 不使用base
//                .setExclude()
                .setInclude(scanner("表名，多个英文逗号分割").split(","));
        codeGen.execute();
    }
}
